﻿<MonitorWindow:TeamMemberBadgeBase
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:charting="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    xmlns:converters="clr-namespace:ScrumSprintMonitor.UI.Wpf.Converters"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:ScrumSprintMonitor.UI.Wpf"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:MonitorWindow="clr-namespace:ScrumSprintMonitor.UI.Wpf.Views.MonitorWindow"
    xmlns:DataVisualization="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    xmlns:chartingprimitives="clr-namespace:System.Windows.Controls.DataVisualization.Charting.Primitives;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    xmlns:ChartingExtensions="clr-namespace:System.Windows.Controls.DataVisualization.Charting"
    mc:Ignorable="d"
    x:Class="ScrumSprintMonitor.UI.Wpf.Views.MonitorWindow.TeamMemberBadge">

    <MonitorWindow:TeamMemberBadgeBase.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="..\..\Styles\ChartResourceDictionary.xaml"/>
                <ResourceDictionary Source="..\..\Styles\TeamMemberBaseStyles.xaml"/>
            </ResourceDictionary.MergedDictionaries>

            <converters:WorkTimeRatioBrushValueConverter x:Key="WorkTimeRatioBrushValueConverter" />

            <local:XamlServiceLocator x:Key="serviceLocator" />

            <Style x:Key="RelevanceStyle" TargetType="{x:Type Control}">
                <Style.Resources>
                    <Storyboard x:Key="FadeOutStoryboard">
                        <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="{x:Null}" Storyboard.TargetProperty="(UIElement.Opacity)">
                            <SplineDoubleKeyFrame KeyTime="00:00:01" Value="0.25"/>
                        </DoubleAnimationUsingKeyFrames>
                    </Storyboard>
                    <Storyboard x:Key="FadeInStoryboard">
                        <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="{x:Null}" Storyboard.TargetProperty="(UIElement.Opacity)">
                            <SplineDoubleKeyFrame KeyTime="00:00:01" Value="1"/>
                        </DoubleAnimationUsingKeyFrames>
                    </Storyboard>
                </Style.Resources>

                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsActiveInSprint}" Value="False">
                        <DataTrigger.EnterActions>
                            <BeginStoryboard Storyboard="{StaticResource FadeOutStoryboard}" />
                        </DataTrigger.EnterActions>
                        <DataTrigger.ExitActions>
                            <BeginStoryboard Storyboard="{StaticResource FadeInStoryboard}" />
                        </DataTrigger.ExitActions>
                    </DataTrigger>
                </Style.Triggers>
            </Style>

            <Style x:Key="BadgeInfoLabel" TargetType="{x:Type TextBlock}" BasedOn="{StaticResource InfoLabel}">
                <Setter Property="HorizontalAlignment" Value="Left" />
                <Setter Property="FontSize" Value="56" />
            </Style>

            <Style x:Key="BadgeNumericValueLabel" TargetType="{x:Type TextBlock}" BasedOn="{StaticResource NumericValueLabel}">
                <Setter Property="HorizontalAlignment" Value="Right" />
                <Setter Property="FontSize" Value="56" />
            </Style>

            <Style x:Key="WorkInfoLabel" TargetType="{x:Type TextBlock}">
                <Setter Property="FontWeight" Value="Light"/>
                <Setter Property="FontSize" Value="18"/>
                <Setter Property="Padding" Value="5"/>
                <Setter Property="HorizontalAlignment" Value="Center"/>
                <Setter Property="VerticalAlignment" Value="Bottom"/>
                <Setter Property="Opacity" Value="0.7"/>
            </Style>

            <Style x:Key="VerticalHeaderLabel" BasedOn="{StaticResource WorkInfoLabel}" TargetType="{x:Type TextBlock}">
                <Setter Property="Padding" Value="5"/>
                <Setter Property="RenderTransformOrigin">
                    <Setter.Value>
                        <Point X="0.5" Y="0.5"/>
                    </Setter.Value>
                </Setter>
                <Setter Property="LayoutTransform">
                    <Setter.Value>
                        <TransformGroup>
                            <ScaleTransform ScaleX="1" ScaleY="1"/>
                            <SkewTransform AngleX="0" AngleY="0"/>
                            <RotateTransform Angle="-90"/>
                            <TranslateTransform X="0" Y="0"/>
                        </TransformGroup>
                    </Setter.Value>
                </Setter>
                <Setter Property="RenderTransform">
                    <Setter.Value>
                        <TransformGroup>
                            <ScaleTransform ScaleX="1" ScaleY="1"/>
                            <SkewTransform AngleX="0" AngleY="0"/>
                            <RotateTransform/>
                            <TranslateTransform X="0" Y="0"/>
                        </TransformGroup>
                    </Setter.Value>
                </Setter>
            </Style>
            
            <Style x:Key="BurndownChartStyle" BasedOn="{StaticResource BaseChartStyle}" TargetType="{x:Type charting:Chart}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type charting:Chart}">
                            <chartingprimitives:EdgePanel x:Name="ChartArea" Style="{TemplateBinding ChartAreaStyle}" />
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="Palette">
                    <Setter.Value>
                        <DataVisualization:ResourceDictionaryCollection>
                            <ResourceDictionary>
                                <Style x:Key="DataPointStyle" TargetType="Control">
                                    <Setter Property="Background">
                                        <Setter.Value>
                                            <RadialGradientBrush GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9">
                                                <GradientStop Color="#FFFCFCFC" />
                                                <GradientStop Color="#FFF0F0F0" Offset="1" />
                                            </RadialGradientBrush>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </ResourceDictionary>
                        </DataVisualization:ResourceDictionaryCollection>
                    </Setter.Value>
                </Setter>
            </Style>

            <DataTemplate x:Key="BurndownChartDataTemplate">
                <charting:Chart
                    Style="{StaticResource BurndownChartStyle}"
                    Margin="12"
                    Opacity="0.5">

                    <charting:AreaSeries Margin="12" ItemsSource="{Binding BurndownDataPoints}" IndependentValueBinding="{Binding Time}" DependentValueBinding="{Binding WorkRemaining}" />

                    <charting:Chart.Axes>
                        <!-- Vertical axis -->
                        <charting:LinearAxis
                                    Orientation="Y"
                                    ExtendRangeToOrigin="True"
                                    MinorTickMarkStyle="{x:Null}"
                                    MajorTickMarkStyle="{x:Null}"
                                    Minimum="0"
                                    Interval="6"
                                    ShowGridLines="False">
                            <charting:LinearAxis.AxisLabelStyle>
                                <Style TargetType="charting:NumericAxisLabel">
                                    <Setter Property="Visibility" Value="Collapsed"/>
                                </Style>
                            </charting:LinearAxis.AxisLabelStyle>
                        </charting:LinearAxis>
                        <!-- Shared horizontal axis -->
                        <ChartingExtensions:CustomDateTimeAxis
                                Orientation="X"
                                Minimum="{Binding BurndownChartStartDate}"
                                Maximum="{Binding BurndownChartEndDate}"
                                DisabledDates="{Binding ExcludedDates}"
                                MinorTickMarkStyle="{x:Null}"
                                MajorTickMarkStyle="{x:Null}"
                                IntervalType="Days"
                                Interval="1">
                            <ChartingExtensions:CustomDateTimeAxis.AxisLabelStyle>
                                <Style TargetType="charting:DateTimeAxisLabel">
                                    <Setter Property="Visibility" Value="Collapsed"/>
                                    <Setter Property="StringFormat" Value="{}{0:MMM d}"/>
                                </Style>
                            </ChartingExtensions:CustomDateTimeAxis.AxisLabelStyle>
                        </ChartingExtensions:CustomDateTimeAxis>
                    </charting:Chart.Axes>
                </charting:Chart>
            </DataTemplate>

            <DataTemplate x:Key="HoursAvailableDataTemplate">
                <TextBlock>
                    <TextBlock.Style>
                      <Style BasedOn="{StaticResource BadgeNumericValueLabel}" TargetType="{x:Type TextBlock}">
                         <Setter Property="Text" Value="{Binding RemainingSprintHours, StringFormat=\{0:N0\}h}"/>

                         <Style.Triggers>
                            <DataTrigger Binding="{Binding IncludesFreeBusyInformation}" Value="False">
                               <Setter Property="Text" Value="{Binding RemainingSprintHours, StringFormat=≤ \{0:N0\}h}"/>
                            </DataTrigger>
                         </Style.Triggers>
                      </Style>
                    </TextBlock.Style>
                </TextBlock>
            </DataTemplate>

            <DataTemplate x:Key="RemainingSprintWorkDataTemplate">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Style="{StaticResource BadgeNumericValueLabel}" FontSize="28" x:Name="SprintWork" VerticalAlignment="Top" Text="{Binding RemainingSprintWork, StringFormat='(\{0:N0\}h) '}" />
                    <TextBlock Style="{StaticResource BadgeNumericValueLabel}" Padding="5,5,0,5" Text="{Binding RemainingSprintWorkWithUnassigned, StringFormat=\{0:N0\}h}" />
                    <TextBlock Text="+" Padding="0,5,5,5">
                        <TextBlock.Style>
                            <Style BasedOn="{StaticResource BadgeNumericValueLabel}" TargetType="{x:Type TextBlock}">
                                <Setter Property="Visibility" Value="Collapsed"/>

                                <Style.Triggers>
                                   <DataTrigger Binding="{Binding NotAllWorkItemsEstimated}" Value="True">
                                       <Setter Property="Visibility" Value="Visible"/>
                                   </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </TextBlock.Style>
                    </TextBlock>
                </StackPanel>

                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding RemainingUnassignedSprintWork}" Value="0">
                        <Setter TargetName="SprintWork" Property="Visibility" Value="Collapsed"/>
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>

            <DataTemplate x:Key="AvatarDataTemplate">
                <Image Source="{Binding AvatarImageSource}" Stretch="Fill" Width="96" Height="96" Margin="0,0,12,0">
                    <Image.Style>
                        <Style TargetType="{x:Type Image}">
                            <Setter Property="Visibility" Value="Visible"/>

                            <Style.Triggers>
                                <DataTrigger Binding="{Binding AvatarImageSource}" Value="{x:Null}">
                                    <Setter Property="Visibility" Value="Collapsed"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Image.Style>

                    <Image.Effect>
                        <DropShadowEffect/>
                    </Image.Effect>
                </Image>
            </DataTemplate>

            <DataTemplate x:Key="PlanningModeBadgeDataTemplate">
                <Grid Margin="32">
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>

                    <Grid Grid.ColumnSpan="3">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>

                        <ContentPresenter Content="{Binding}" ContentTemplate="{StaticResource AvatarDataTemplate}"/>

                        <TextBlock FontSize="64" Grid.Column="1" Style="{StaticResource TeamMemberNameLabel}" Text="{Binding DisplayName}"/>
                    </Grid>

                    <TextBlock Grid.Column="0" Grid.Row="1" Style="{StaticResource BadgeInfoLabel}"><Run Text="Open Backlog Items:"/></TextBlock>
                    <TextBlock Grid.Column="2" Grid.Row="1" Style="{StaticResource BadgeNumericValueLabel}" Text="{Binding RemainingWorkItemCount, StringFormat=N0}"/>

                    <TextBlock Grid.Row="2" Style="{StaticResource BadgeInfoLabel}"><Run Text="Estimated Workload:"/></TextBlock>
                    <charting:Chart
                        Width="96"
                        Height="96"
                        Grid.Column="1"
                        Grid.Row="2"
                        Style="{StaticResource PieChartStyle}">
                        <charting:PieSeries Margin="6" ItemsSource="{Binding WorkloadPieChartData}" IndependentValueBinding="{Binding ValueName}" DependentValueBinding="{Binding Value}" />
                    </charting:Chart>
                    <Viewbox x:Name="DoneWork_SprintWork"
                             Grid.Column="2"
                             Grid.Row="2"
                             HorizontalAlignment="Right">
                        <ContentPresenter x:Name="RemainingWorkload"
                                          HorizontalAlignment="Right"
                                          Content="{Binding}"
                                          ContentTemplate="{StaticResource RemainingSprintWorkDataTemplate}"/>
                    </Viewbox>

                    <TextBlock Grid.Row="3" Style="{StaticResource BadgeInfoLabel}" Text="Hours Available:"/>
                    <ContentPresenter Grid.Column="2" Grid.Row="3" HorizontalAlignment="Right" Content="{Binding}" ContentTemplate="{StaticResource HoursAvailableDataTemplate}"/>
                </Grid>
            </DataTemplate>

            <DataTemplate x:Key="SprintModeBadgeDataTemplate">
                <Grid Margin="32">
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition Height="6"/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="3*"/>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                        <ColumnDefinition Width="6"/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>

                    <!--Display a question mark for team members that have not estimated all their tasks-->
                    <Viewbox Grid.Column="4" Grid.RowSpan="5" Panel.ZIndex="-100">
                        <Viewbox.Style>
                            <Style TargetType="{x:Type Viewbox}">
                                <Setter Property="Visibility" Value="Collapsed"/>

                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding NotAllWorkItemsEstimated}" Value="True">
                                        <Setter Property="Visibility" Value="Visible"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Viewbox.Style>

                        <TextBlock Text="?" Opacity="0.25" />
                    </Viewbox>

                    <!--Display avatar and name-->
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>

                        <ContentPresenter Content="{Binding}" ContentTemplate="{StaticResource AvatarDataTemplate}"/>

                        <TextBlock FontSize="64" Grid.Column="1" Style="{StaticResource TeamMemberNameLabel}" Text="{Binding DisplayName}" />
                    </Grid>

                    <!--Display vertical Done column name-->
                    <TextBlock FontSize="36" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="0" Style="{StaticResource VerticalHeaderLabel}" Text="Done"/>
                    <!--Display vertical Open column name-->
                    <TextBlock FontSize="36" Grid.Column="4" Grid.Row="0" Style="{StaticResource VerticalHeaderLabel}" Text="Open"/>

                    <Border Grid.RowSpan="4" Grid.Column="3" BorderBrush="White" BorderThickness="1" Width="1" OpacityMask="{StaticResource LinearSeparatorLineBrush}" />

                    <!--Display Backlog items row name-->
                    <TextBlock Grid.Column="0" Grid.Row="1" Style="{StaticResource BadgeInfoLabel}" Text="Backlog Items:"/>
                    <!--Display Backlog items row value-->
                    <TextBlock Grid.Row="1" Grid.Column="1" x:Name="DoneWork_WorkItemCount" Style="{StaticResource BadgeNumericValueLabel}" Text="{Binding DoneWorkItemCount, StringFormat=N0}"/>
                    <charting:Chart Width="96"
                                    Height="96"
                                    Grid.Column="2" Grid.Row="1"
                                    Style="{StaticResource PieChartStyle}">
                        <charting:PieSeries Margin="12" ItemsSource="{Binding BacklogItemsPieChartData}" IndependentValueBinding="{Binding ValueName}" DependentValueBinding="{Binding Value}" />
                    </charting:Chart>
                    <!--Display remaining Backlog items row value-->
                    <TextBlock Grid.Column="4" Grid.Row="1" Style="{StaticResource BadgeNumericValueLabel}" Text="{Binding RemainingWorkItemCount, StringFormat=N0}"/>

                    <!--Display Work row name-->
                    <TextBlock Grid.Row="2" Style="{StaticResource BadgeInfoLabel}" Text="Work:"/>
                    <!--Display Work row value-->
                    <TextBlock Grid.Row="2" Grid.Column="1" x:Name="DoneWork_SprintWork" Style="{StaticResource BadgeNumericValueLabel}" Text="{Binding DoneSprintWork, StringFormat=\{0:N0\}h}"/>
                    <charting:Chart
                        Width="96"
                        Height="96"
                        Grid.Column="2"
                        Grid.Row="2"
                        Style="{StaticResource PieChartStyle}">
                        <charting:PieSeries Margin="12" ItemsSource="{Binding WorkloadPieChartData}" IndependentValueBinding="{Binding ValueName}" DependentValueBinding="{Binding Value}" />
                    </charting:Chart>
                    <!--Display remaining Work row value-->
                    <ContentPresenter Grid.Column="4" Grid.Row="2" HorizontalAlignment="Right" Content="{Binding}" ContentTemplate="{StaticResource RemainingSprintWorkDataTemplate}"/>

                    <Border Grid.Row="3" Grid.ColumnSpan="5" BorderBrush="White" BorderThickness="1" VerticalAlignment="Bottom" OpacityMask="{StaticResource SeparatorLineBrush}" />

                    <!--Display Time Available row name-->
                    <TextBlock Grid.Row="4" Style="{StaticResource BadgeInfoLabel}" Text="Time Available:"/>
                    <!--Display Time Available row value-->
                    <ContentPresenter Grid.Column="4" Grid.Row="4" HorizontalAlignment="Right" Content="{Binding}" ContentTemplate="{StaticResource HoursAvailableDataTemplate}"/>

                    <!--Display burndown chart-->
                    <ContentPresenter Panel.ZIndex="-1"
                        Grid.ColumnSpan="5"
                        Grid.Row="1"
                        Grid.RowSpan="4"
                        Content="{Binding}"
                        Visibility="{Binding BurndownChartVisibility}">
                        <ContentPresenter.Style>
                            <Style TargetType="{x:Type ContentPresenter}">
                                <Setter Property="ContentTemplate">
                                    <Setter.Value>
                                        <DataTemplate/>
                                    </Setter.Value>
                                </Setter>

                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding BurndownChartVisibility}" Value="Visible">
                                        <Setter Property="ContentTemplate" Value="{StaticResource BurndownChartDataTemplate}"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </ContentPresenter.Style>
                    </ContentPresenter>
                </Grid>
            </DataTemplate>
        </ResourceDictionary>
    </MonitorWindow:TeamMemberBadgeBase.Resources>

    <!--<MonitorWindow:TeamMemberBadgeBase.DataContext>
        <Binding Path="TeamMembersListViewModel.TeamMembersIncludingUnassigned[1]" Source="{StaticResource serviceLocator}"/>
    </MonitorWindow:TeamMemberBadgeBase.DataContext>-->

    <MonitorWindow:TeamMemberBadgeBase.Effect>
        <!-- Create a drop shadow. This is a very expensive effect, very noticeable in the animations. -->
        <DropShadowEffect BlurRadius="5" Color="Black" ShadowDepth="10" Opacity="0.3" />
    </MonitorWindow:TeamMemberBadgeBase.Effect>

    <MonitorWindow:TeamMemberBadgeBase.Style>
        <StaticResource ResourceKey="RelevanceStyle"/>
    </MonitorWindow:TeamMemberBadgeBase.Style>

    <Grid>
        <!-- Glow-on-update element -->
        <Border Style="{StaticResource GlowOnUpdateStyle}" BorderThickness="0" Background="Black" CornerRadius="2,32,2,32" />

        <Border x:Name="border" Loaded="OnGlassPanelLoaded" BorderBrush="#7F000000" BorderThickness="1,1,1,1" CornerRadius="2,32,2,32">
            <Border.Background>
                <MultiBinding Converter="{StaticResource WorkTimeRatioBrushValueConverter}" ConverterParameter="Background">
                    <Binding Path="RemainingSprintWorkWithUnassigned"/>
                    <Binding Path="RemainingSprintHours"/>
                    <Binding Path="NotAllWorkItemsEstimated"/>
                </MultiBinding>
            </Border.Background>

            <Grid>
                <ContentPresenter Content="{Binding}">
                    <ContentPresenter.Style>
                        <Style TargetType="{x:Type ContentPresenter}">
                            <Setter Property="TextBlock.Foreground">
                                <Setter.Value>
                                    <MultiBinding Converter="{StaticResource WorkTimeRatioBrushValueConverter}" ConverterParameter="Foreground">
                                        <Binding Path="RemainingSprintWorkWithUnassigned"/>
                                        <Binding Path="RemainingSprintHours"/>
                                        <Binding Path="NotAllWorkItemsEstimated"/>
                                    </MultiBinding>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="ContentTemplate" Value="{StaticResource SprintModeBadgeDataTemplate}"/>

                            <Style.Triggers>
                                <DataTrigger Binding="{Binding MonitorViewModel.SprintMode, Source={StaticResource serviceLocator}}" Value="Planning">
                                    <Setter Property="ContentTemplate" Value="{StaticResource PlanningModeBadgeDataTemplate}"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </ContentPresenter.Style>
                </ContentPresenter>

                <ContentPresenter ContentTemplate="{StaticResource GlassPanel}"/>
            </Grid>
        </Border>
    </Grid>
</MonitorWindow:TeamMemberBadgeBase>